package org.davidtrevien.com.imput.omi.panels.center.planning.model;

import org.jdesktop.swingx.treetable.DefaultMutableTreeTableNode;
import org.jdesktop.swingx.treetable.DefaultTreeTableModel;
import org.jdesktop.swingx.treetable.TreeTableNode;

public class TOsTasksModel 
	extends DefaultTreeTableModel {

	public static final int NAME = 0;
	public static final int ACTIVITYNAME = 1;
	public static final int TASKBUDGET = 2;
	public static final int TASKFINALCOST = 3;
	public static final int TASKRAF = 4;
	public static final int TASKCONSO = 5;
	public static final int TASKPLAN = 6;

	public static final int OS = 7;
	public static final int TASK = 8;
	public static final int TASKLEVEL = 9;
	public static final int TASKSTATE = 10;
	public static final int TASKDELIVERYREQ = 11;
	public static final int TASKDELIVERYEFF = 12;
	
	public TOsTasksModel(TreeTableNode root) {
		super(root);
	}
	
	@Override
	public int getColumnCount() {
		return 7;
	}

	/**
	 * Returns which object is displayed in this column.
	 */
	@Override
	public Object getValueAt(Object node, int column) {
		Object res = null;
		if (node instanceof DefaultMutableTreeTableNode) {
			DefaultMutableTreeTableNode defNode = (DefaultMutableTreeTableNode) node;
			if (defNode.getUserObject() instanceof TOs) {
				TOs tos = (TOs) defNode.getUserObject();
				switch (column) {
				case NAME:
					res = tos.getName();
					break;
				case OS:
					res = tos.getOs();
					break;
				case TASK:
					res = tos.getTask();
					break;
				}
			}
			else if (defNode.getUserObject() instanceof TTask) {
				TTask ttask = (TTask) defNode.getUserObject();
				switch (column) {
				case NAME:
					res = ttask.getTos().getName();
					break;
				case OS:
					res = ttask.getTos().getOs();
					break;
				case TASK:
					res = ttask.getTos().getTask();
					break;
				case ACTIVITYNAME:
					res = ttask.getLabel();
					break;
				case TASKBUDGET:
					res = ttask.getBudget();
					break;
				case TASKCONSO:
					res = ttask.getConsomated();
					break;
				case TASKDELIVERYEFF:
					res = ttask.getDeliveryEffectiveDate();
					break;
				case TASKDELIVERYREQ:
					res = ttask.getDeliveryRequieredDate();
				case TASKFINALCOST:
					res = ttask.getFinalCost();
					break;
				case TASKLEVEL:
					res = ttask.getLevel();
					break;
				case TASKPLAN:
					res = ttask.getPlan();
					break;
				case TASKRAF:
					res = ttask.getRaf();
					break;
				//case TASKSTATE:
				//	res = ttask.getState();
				//	break;
				}
			}
		}
		return res != null ? res : "";
	}
	
	/**
	 * What the TableHeader displays when the Table is in a JScrollPane.
	 */
	@Override
	public String getColumnName(int column) {
		String res = "";
		switch (column) {
		case NAME:
			res = "Projet";
			break;
		case OS:
			res = "OS";
			break;
		case TASK:
			res = "Tâche";
			break;
		case ACTIVITYNAME:
			res = "Activité";
			break;
		case TASKBUDGET:
			res = "Budget";
			break;
		case TASKCONSO:
			res = "Consommé";
			break;
		case TASKDELIVERYEFF:
			res = "Livraison (Eff.)";
			break;
		case TASKDELIVERYREQ:
			res = "Livraison (Plan.)";
		case TASKFINALCOST:
			res = "Coût final";
			break;
		case TASKLEVEL:
			res = "Niveau";
			break;
		case TASKPLAN:
			res = "Plannifié";
			break;
		case TASKRAF:
			res = "Ràf";
			break;
		case TASKSTATE:
			res = "Etat";
			break;
		}
		return res;
	}

	/**
	 * Tells if a column can be edited.
	 */
	@Override
	public boolean isCellEditable(Object node, int column) {
		return true;
	}

	/**
	 * Called when done editing a cell.
	 */
	@Override
	public void setValueAt(Object value, Object node, int column) {
		if (node instanceof DefaultMutableTreeTableNode) {
			DefaultMutableTreeTableNode defNode = (DefaultMutableTreeTableNode) node;
			if (defNode.getUserObject() instanceof TOs) {
				TOs tos = (TOs) defNode.getUserObject();
				switch (column) {
				case NAME:
					tos.setName(value.toString());
					break;
				case TASK:
					tos.setTask(value.toString());
					break;
				case OS:
					tos.setOs(value.toString());
					break;
				}
			}
			else if (defNode.getUserObject() instanceof TTask) {
				TTask ttask = (TTask) defNode.getUserObject();
				switch (column) {
				case NAME:
					ttask.getTos().setName(value.toString());
					break;
				case TASK:
					ttask.getTos().setTask(value.toString());
					break;
				case OS:
					ttask.getTos().setOs(value.toString());
					break;
				case ACTIVITYNAME:
					ttask.setLabel(value.toString());
					break;
				}
			}
		}
	}
}
